package solution;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution {
    public ListNode sortList(ListNode head) {
        List<Integer> list = new ArrayList<>();

        ListNode cur = head;
        while (cur != null) {
            list.add(cur.val);
            cur = cur.next;
        }

        Collections.sort(list);

        ListNode newHead = new ListNode();
        cur = newHead;
        for (int num : list) {
            ListNode node = new ListNode(num);
            cur.next = node;
            cur = cur.next;
        }

        return newHead.next;
    }
}